
#ifndef _UTIL_THREAD_H_
#define _UTIL_THREAD_H_

#include"xtypes.h"
#include"util_sync.h"


enum e_xthread_status
{
	XTHREAD_STATUS_INVALID,
	XTHREAD_STATUS_RUN,
	XTHREAD_STATUS_STOP,
	XTHREAD_STATUS_SUSPEND,
};

typedef uint_t  (__CDECL *t_xThreadAddress)(void *);
typedef void  (__CDECL *t_xThreadEventFuncAddress)();

class CxThread : public CxCriticalSection
{
public:
	bool_t IsRunning();
	xResult Run();

	//Quit Violently
	xResult Terminate( uint_t _Retval );

	bool_t IsSuspended();
	xResult Suspend();
	xResult Resume();

	void SetThreadAddr( t_xThreadAddress _pThreadAddr );
	void SetArgList(wildptr_t _pArgList );
	void SetBeginEventFunc(t_xThreadEventFuncAddress _pOnThreadBegin);
	void SetEndEventFunc(t_xThreadEventFuncAddress _pOnThreadEnd);
	void SetStackSize( int_t _StackSize );

	wildptr_t GetArgList();
	int_t GetThreadStatus();
	int_t GetStackSize();
	uint_t GetThreadHandle();
	uint_t GetThreadId();

public:
	static CxThread * Create( t_xThreadAddress _ThreadAddr = NULL, wildptr_t _ArgList = NULL, t_xThreadEventFuncAddress _BeginEvent = NULL, t_xThreadEventFuncAddress _EndEvent = NULL );
	virtual void Release();

public:
	CxThread();
	virtual ~CxThread();

protected:
	void _Init();
	void _Alloc();
	void _Tidy();

	//used to overload
	uint_t ThreadProc();
	static uint_t __STDCALL _ThreadProc(void * _Arg);

	void _OnThreadBegin();
	void _OnThreadEnd();

protected:	
	volatile int_t m_ThreadStatus;

	t_xThreadAddress m_pThreadAddr;
	wildptr_t m_pArgList;
	int_t m_nStackSize;

	uint_t m_ThreadHandle;
	uint_t m_ThreadId;

	t_xThreadEventFuncAddress m_pOnThreadBegin;
	t_xThreadEventFuncAddress m_pOnThreadEnd;
};



/*---------------------------------------------------------
Functions
---------------------------------------------------------*/
void xSleepThread(ulong_t _Ms);
void xExitThread( uint_t _Retval );








#endif //_UTIL_THREAD_H_




